package com.vm.utils;

import java.util.HashMap;

import android.content.Context;
import android.os.Handler;
import com.vm.db.DownInfoDao;

public class ThreadManager {

	public static HashMap<String, DownloadThread> hashMap;

	public static DownInfoDao dao;

	public ThreadManager(Context context, Handler mainHandler) {

		// this.mainHandler = mainHandler;
	}

	public static DownloadThread getDownloadThread(Context context,
			String softId, Handler mainHandler) {
		DownloadThread downloadThread;
		if (dao == null) {
			dao = new DownInfoDao(context);
		}

		if (hashMap == null) {
			hashMap = new HashMap<String, DownloadThread>();
		}

		if (hashMap.containsKey(softId)) {
			downloadThread = hashMap.get(softId);
			downloadThread.setMainHandler(dao, mainHandler);
		} else {
			downloadThread = new DownloadThread(dao, mainHandler);
			downloadThread.start();
			hashMap.put(softId, downloadThread);
		}

		return downloadThread;

	}

	public static DownloadThread getDownloadThread2(Context context,
			String softId, Handler thirdHandler) {
		DownloadThread downloadThread;
		if (dao == null) {
			dao = new DownInfoDao(context);
		}

		if (hashMap == null) {
			hashMap = new HashMap<String, DownloadThread>();
		}

		if (hashMap.containsKey(softId)) {
			downloadThread = hashMap.get(softId);
			downloadThread.setThirdHandler(dao, thirdHandler);
		} else {
			downloadThread = new DownloadThread(dao, thirdHandler);
			downloadThread.start();
			hashMap.put(softId, downloadThread);
			
		}

		return downloadThread;

	}

	public static void stopSendMessage(String softId) {

		DownloadThread downloadThread;

		if (hashMap == null) {
			hashMap = new HashMap<String, DownloadThread>();
		}

		if (hashMap.containsKey(softId)) {
			downloadThread = hashMap.get(softId);
			downloadThread.setThirdHandler(dao, null);
		}
	}

	/**
	 * @param startPosition
	 * @param httpURLConnection
	 * @param info
	 * @param file
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	// private void startDown(String fileUrl, Soft soft)
	// throws FileNotFoundException, IOException {
	//
	// URL url = null;
	// url = new URL(fileUrl);
	// HttpURLConnection httpURLConnection = (HttpURLConnection) url
	// .openConnection();
	//
	// File file = new File(Environment.getExternalStorageDirectory()
	// .getPath() + "/vmclient/" + soft.getName() + ".apk");
	// // if(!file.exists()){
	//
	// File file2 = new File(Environment.getExternalStorageDirectory()
	// .getPath() + "/vmclient/");
	// // 判断是否存在父目录，如果没有，自动创建一个
	// if (!file2.exists()) {
	// }
	// {
	// file.getParentFile().mkdir();
	// }
	//
	// if (!file.exists()) {
	// file.createNewFile();
	// } else {
	// if (file.isDirectory()) {
	// file.delete();
	// file.createNewFile();
	// }
	// }
	//
	// /**
	// * 查询数据库中是否有此下载信息
	// */
	// DownInfo info = dao.query(soft.getId());
	//
	// if (info != null) {
	// // 如果有, 读取当前线程已下载量
	// if (info.getCompleted() == 1) {
	// info.setCompleted(0);
	// done = 0;
	// } else {
	// done += info.getDone();
	// }
	// // isPause = info.getIsPause() == 0 ? false : true;
	// } else {
	// // 如果没有, 则创建一个新记录存入
	// // info = new Info(file.getAbsolutePath(), soft.getId(), 0, 0,
	// // FileSize);
	// info = new DownInfo();
	// info.setPath(file.getAbsolutePath());
	// info.setSoftId(soft.getId());
	// info.setName(soft.getName());
	// info.setImageUrl(soft.getImageUrl());
	// info.setIsPause(0);
	// info.setDone(0);
	// info.setApkUrl(soft.getApkUrl());
	// info.setApkLength(FileSize);
	// dao.insert(info);
	// }
	// // 指定开始与结束的下载位置
	//
	// httpURLConnection.setRequestProperty("Range", "bytes=" + done + "-"
	// + (FileSize - 1));
	// RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
	// randomAccessFile.seek(info.getDone());
	// InputStream inputStream = httpURLConnection.getInputStream();
	// byte[] buffer = new byte[1024 * 10];
	// int count = 0;
	//
	// while ((count = inputStream.read(buffer)) != -1) {
	// if (isPause) {
	// // 使用线程锁锁定该线程
	// synchronized (dao) {
	// try {
	// dao.wait();
	// } catch (InterruptedException e) {
	// e.printStackTrace();
	// }
	// }
	// }
	// randomAccessFile.write(buffer, 0, count);
	// done += count;
	// info.setDone(info.getDone() + count);
	// // 记录每个线程已下载的数据量
	// dao.update(info);
	// // 新线程中用Handler发送消息，主线程接收消息
	// Message msg = new Message();
	// msg.what = 600;
	// msg.arg1 = done;
	// mainHandler.sendMessage(msg);
	// }
	//
	// // 循环结束，APK下载完成
	// // 新线程中用Handler发送消息，主线程接收消息
	// Message msg = new Message();
	// msg.what = 1000;
	// msg.arg1 = done;
	// mainHandler.sendMessage(msg);
	//
	// inputStream.close();
	// randomAccessFile.close();
	// httpURLConnection.disconnect();
	//
	// }

}
